package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.StrategyContent;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.domain.DestinationTemplate;
import cn.wolfcode.luowowo.search.domain.StrategyTemplate;
import cn.wolfcode.luowowo.search.domain.TravelTemplate;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.ISearchService;
import cn.wolfcode.luowowo.search.service.IStrategySearchService;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.search.vo.StatisVO;
import cn.wolfcode.luowowo.website.web.annotation.RequireLogin;
import cn.wolfcode.luowowo.website.web.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.naming.directory.SearchResult;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/strategy")
public class StrategyController {

    @Reference
    private IStrategyDetailService detailService;
   @Reference
   private IDestinationService destinationService;
   @Reference
   private IStrategyTagService strategyTagService;
   @Reference
   private IStrategyCommentService strategyCommentService;
   @Reference
   private IStrategyStatisVOService statisVOService;
   @Reference
   private IStrategyCommendService commendService;
   @Reference
   private IStrategySearchService strategySearchService;
   @Reference
   private ISearchService searchService;


    @RequestMapping("")
    public String index(Model model){
        //commends
        List<StrategyCommend> commends = commendService.getTop5();
        model.addAttribute("commends",commends);

        //hotCds    热门攻略推荐排行
        List<StrategyStatisVO> hotCds = statisVOService.queryHotCommends();
        model.addAttribute("hotCds",hotCds);

        //abroadCds   国外推荐排行
        model.addAttribute("abroadCds",statisVOService.queryAbroadCommends());
        //unabroadCds    国内推荐排行
        model.addAttribute("unabroadCds",statisVOService.queryChinaCommends());

        //themeCds    主题推荐排行
        List<Map<String,Object>> list = strategySearchService.queryThemeCommends();
        model.addAttribute("themeCds",list);

        //themes    主题攻略条件列表
        List<StatisVO> themes = strategySearchService.queryConditionThemes();
        model.addAttribute("themes",themes);

        //chinas    国内攻略
        model.addAttribute("chinas",strategySearchService.queryConditionProvinces());

        //abroads   国外攻略
        model.addAttribute("abroads",strategySearchService.queryConditionCountries());

        return "strategy/index";
    }


    @RequestMapping("/searchPage")
    public String searchPage(Model model,SearchQueryObject qo){

        //page
        model.addAttribute("page",strategySearchService.query(qo));


        return "strategy/searchPageTpl";
    }

    @RequestMapping("/detail")
    public String detail(Long id, Model model,@UserParam UserInfo userInfo){

        //detail
        StrategyDetail detail = detailService.get(id);
        StrategyContent content = detailService.getContent(id);
        detail.setStrategyContent(content);
        model.addAttribute("detail",detail);

        //每次访问攻略明细,阅读数加1
        statisVOService.incrViewnum(id,1);

        //热门攻略排序加分  阅读数 +1
        statisVOService.addHotScores(id,1);

        //vo    统计数据对象
        model.addAttribute("vo",statisVOService.getStatisVO(id));

        //isFavor   是否收藏
        boolean ret = userInfo != null ? statisVOService.isFavor(id,userInfo.getId()):false;
        model.addAttribute("isFavor",ret);
        return "strategy/detail";
    }

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyDetailQuery qo){

        //tags  所有标签
        model.addAttribute("tags",strategyTagService.list());

        //pageInfo  明细分页列表
        model.addAttribute("pageInfo",detailService.query(qo));

        //toasts    吐丝查询
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());

        Destination destination = toasts.remove(toasts.size() - 1);

        model.addAttribute("toasts", toasts);
        model.addAttribute("dest", destination);


        return "strategy/list";
    }

    @RequestMapping("/commentAdd")
    @ResponseBody
    @RequireLogin
    public Object commentAdd(StrategyComment comment, @UserParam UserInfo userInfo){
        comment.setHeadUrl(userInfo.getHeadImgUrl());
        comment.setUsername(userInfo.getNickname());
        comment.setUserId(userInfo.getId());
        comment.setCity(userInfo.getCity());
        comment.setLevel(userInfo.getLevel());
        strategyCommentService.save(comment);


        //热门攻略排序加分  评论数 +1
        statisVOService.addHotScores(comment.getDetailId(),1);
        //评论数 +1
        statisVOService.incrReplynum(comment.getDetailId(),1);
        int replynum = statisVOService.getStatisVO(comment.getDetailId()).getReplynum();

        //返回评论数给页面
        return new AjaxResult().addData(replynum);
    }


    //评论列表
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") StrategyCommentQuery qo){

        //page
        Page page = strategyCommentService.query(qo);
        model.addAttribute("page",page);

        return "strategy/commentTpl";
    }


    //攻略评论点赞
    @RequestMapping("/commentThumbUp")
    @ResponseBody
    public Object commentThumbUp(String toid,Long fromid){

        strategyCommentService.commentThumbUp(toid,fromid);

        return new AjaxResult();
    }


    //攻略收藏
    @RequestMapping("/favor")
    @ResponseBody
    public Object favor(Long sid,@UserParam UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        if (userInfo == null){
            //没有登录
            result.setCode(AjaxResult.NO_LOGIN_CODE);
            result.mark("请先登录");
            return result;
        }
        //true表示收藏操作
        boolean ret = statisVOService.favor(sid, userInfo.getId());

        //攻略排行  +1
        statisVOService.addCommendScores(sid,ret ? 1 : -1);

        //查询当前收藏数
        return  ret ? result.addData(statisVOService.getStatisVO(sid)) : result.addData1(statisVOService.getStatisVO(sid));
    }


    //攻略顶操作
    @RequestMapping("/strategyThumbup")
    @ResponseBody
    public Object strategyThumbup(Long sid,@UserParam UserInfo userInfo){
        AjaxResult result = new AjaxResult();
        if (userInfo == null){
            //没有登录
            result.setCode(AjaxResult.NO_LOGIN_CODE);
            result.mark("请先登录");
            return result;
        }
        //true表示顶成功操作
        boolean ret = statisVOService.strategyThumbup(sid, userInfo.getId());

        //顶操作   攻略推荐 +1
        if (ret){
            statisVOService.addCommendScores(sid,1);
        }

        //查询当前被顶数
        return  ret ? result.addData(statisVOService.getStatisVO(sid)) : result.addData1(statisVOService.getStatisVO(sid));
    }


}
